package com.heatingcharge.security.handle;

import cn.hutool.http.Status;
import com.heatingcharge.common.exception.BaseException;
import com.heatingcharge.common.util.R;
import com.heatingcharge.security.util.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * Description: 认证失败处理类 返回401
 * User: Devin
 * Date: 2019-07-05
 * Time: 11:54
 */
@Slf4j
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {
    private static final long serialVersionUID = -8970718410437077606L;
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e)
            throws IOException, ServletException {
        log.error("请求访问: " + request.getRequestURI() + " 接口， 经jwt认证失败，无法访问系统资源.");
       SecurityUtil.writeJavaScript(R.error(Status.HTTP_UNAUTHORIZED,"请求访问:" + request.getRequestURI() + "接口,经jwt 认证失败,无法访问系统资源"),response);
        //throw new BaseException("请求访问:" + request.getRequestURI() + "接口,经jwt 认证失败,无法访问系统资源",Status.HTTP_UNAUTHORIZED);
    }
}
